package com.schedualnew.mq.config;

import com.schedualnew.mq.util.DateUtils;
import com.schedualnew.mq.util.JsonUtil;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author ljy aka ryan
 * @date 2023/3/20$ 16:45$
 * @description 放射科配置信息
 */
@Component
@ConfigurationProperties(prefix = "xray-room")
@Data
public class XRayMQConfig {

    private static final Logger logger = LogManager.getLogger(XRayMQConfig.class);

    /***
     * 监听管理队列-职工字典 是否启用
     */
    private Boolean enable;

    private String userName;
    private String password;

    private String sourceSystemCode;
    private String hospCode;


    // 获取日期排班
    private String getPACSSourceChannelId;
    private String getPACSSourceChangeMQ;
    private String getPACSTargetSysCode;
    private String getPACSSourceFid;
    private String getPACSSourceOrderNo;

    // 推送预约消息
    private String pushPACSScheduleChannelId;
    private String pushPACSScheduleChangeMQ;
    private String pushPACSTargetSysCode;
    private String pushPACSScheduleFid;
    private String pushPACSScheduleOrderNo;
//
//    /**
//     * 需要监听的科室检查 2AC01304001101,2AC01304001201
//     */
//    private String deptCodes;

    /**
     * 检查室对应的预约规则ID
     */
    private String regularMap;

    /**
     * 放射科的检查类别
     */
    private String examClass;

    /**
     * 时段划分
     */
    private String apmFlags;

    /**
     * 超声科配置
     */
    private CSConfig cs;

    public List<RegularQueueMap> getRegularMaps() {
        List<RegularQueueMap> result = new ArrayList<>();
        if (StringUtils.isBlank(regularMap)) {
            return result;
        }
        try {
            result = JsonUtil.json2List(regularMap, RegularQueueMap.class);
        } catch (Exception e) {
            logger.error("放射科：解析配置文件错误!", e);
        }
        return result;
    }

    /**
     * "3"   全天时段
     * "0"  上午
     * "4"  中午
     * "1"  下午
     * "2"  晚上
     *
     * @param startTime
     * @return
     */
    public String getApmCode(String startTime) {
        startTime = addSecondStr(startTime);
        String result = "3";
        try {
            if (StringUtils.isNotBlank(apmFlags)) {
                String start = DateUtils.getCurrDate() + " " + startTime;
                Date startDate = DateUtils.parse(start, DateUtils.PARRTEN_DATE_TIME);
                List<ApmFlagItem> apmFlagItems = JsonUtil.json2List(apmFlags, ApmFlagItem.class);
                if (CollectionUtils.isNotEmpty(apmFlagItems)) {
                    ApmFlagItem apmFlagItem = apmFlagItems.stream().filter(x -> {
                        if (StringUtils.isNotBlank(x.getStart()) && StringUtils.isNotBlank(x.getEnd())) {
                            Date startXTime = DateUtils.parse(DateUtils.getCurrDate() + " " + addSecondStr(x.getStart()), DateUtils.PARRTEN_DATE_TIME);
                            Date endXTime = DateUtils.parse(DateUtils.getCurrDate() + " " + addSecondStr(x.getEnd()), DateUtils.PARRTEN_DATE_TIME);

                            return (startXTime.before(startDate) || startXTime.equals(startDate)) && endXTime.after(startDate);
                        }
                        return false;
                    }).findFirst().orElse(null);
                    if (Objects.nonNull(apmFlagItem)) {
                        result = apmFlagItem.getCode();
                    }
                }
            }
        } catch (Exception e) {
            logger.error("根据配置获取时间午别异常", e);
        }
        return result;
    }

    private String addSecondStr(String timeStr) {
        if (timeStr.length() == 5) {
            timeStr += ":00";
        }
        return timeStr;
    }

    @Data
    public static class CSConfig {
        /**
         * 超声科检查类别名称
         */
        private String examClass;

        /**
         * 号源同步配置
         */
        private CSSourceConfig source;

    }

    @Data
    public static class CSSourceConfig {
        private Boolean enable;
        private String regularMap;
        private String channelId;
        private String changeMQ;
        private String targetSysCode;
        private String fid;
        private String orderNo;


        public List<RegularQueueMap> getRegularMaps() {
            List<RegularQueueMap> result = new ArrayList<>();
            if (StringUtils.isBlank(regularMap)) {
                return result;
            }
            try {
                result = JsonUtil.json2List(regularMap, RegularQueueMap.class);
            } catch (Exception e) {
                logger.error("超声解析配置文件错误!", e);
            }
            return result;
        }
    }

    @Data
    public static class RegularQueueMap {
        private String queueName;
        private List<RegularMapApmItem> apmMap;
        private String weeks;
    }

    @Data
    public static class RegularMapApmItem {
        private String startTime;
        private String regularId;
        private String weeks;

        private List<Integer> weekIds;

        /**
         * 由weeks转换来的
         *
         * @return
         */
        public List<Integer> getWeekIds() {
            if (CollectionUtils.isEmpty(weekIds) && StringUtils.isNotBlank(weeks)) {
                weekIds = JsonUtil.json2List(weeks, Integer.class);
            }
            return weekIds;
        }
    }


    /**
     * 时段配置
     */
    @Data
    public static class ApmFlagItem {
        private String code;
        private String name;
        private String start;
        private String end;
    }


    public static void main(String[] args) {
//        0:上午 1:下午 2:晚上 4:中午 3全天时段
        String apmFlags = "[{'start':'07:00','end':'12:00','code':'1','name':'上午'},\n" +
                "              {'start':'12:00','end':'14:00','code':'4','name':'中午'},\n" +
                "              {'start':'14:00','end':'18:00','code':'1','name':'下午'}\n" +
                "              {'start':'18:00','end':'22:00','code':'1','name':'晚上'}]";


        String startTime = addSecondStr2("07:00");


        String result = "3";
        if (StringUtils.isNotBlank(apmFlags)) {
            String start = DateUtils.getCurrDate() + " " + startTime;
            Date startDate = DateUtils.parse(start, DateUtils.PARRTEN_DATE_TIME);
            List<ApmFlagItem> apmFlagItems = JsonUtil.json2List(apmFlags, ApmFlagItem.class);
            if (CollectionUtils.isNotEmpty(apmFlagItems)) {
                ApmFlagItem apmFlagItem = apmFlagItems.stream().filter(x -> {
                    if (StringUtils.isNotBlank(x.getStart()) && StringUtils.isNotBlank(x.getEnd())) {
                        Date startXTime = DateUtils.parse(DateUtils.getCurrDate() + " " + addSecondStr2(x.getStart()), DateUtils.PARRTEN_DATE_TIME);
                        Date endXTime = DateUtils.parse(DateUtils.getCurrDate() + " " + addSecondStr2(x.getEnd()), DateUtils.PARRTEN_DATE_TIME);

                        return (startXTime.before(startDate) || startXTime.equals(startDate)) && (endXTime.after(startDate));
                    }
                    return false;
                }).findFirst().orElse(null);
                if (Objects.nonNull(apmFlagItem)) {
                    result = apmFlagItem.getCode();
                }
            }
        }
        System.out.println("result = " + result);
    }

    private static String addSecondStr2(String timeStr) {
        if (timeStr.length() == 5) {
            timeStr += ":00";
        }
        return timeStr;
    }

}
